/*
 * Created on Apr 14, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author U678864
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

package com.jjprojects.earutility;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

public class EARUtility implements ActionListener
{
    static Cursor curHand = new Cursor(Cursor.HAND_CURSOR);
    
    static Logger log4jEARUtility = null; 
    static JFrame frmEARUtility = null;
    JDialog dlgEarCompareFileCollect = null;
    
    RoundTextField txtFirstEARFile = null;
    RoundTextField txtSecondEARFile = null;
    
    JButton btnBrowseFirstEarFile = null;
    JButton btnBrowseSecondEarFile = null;
    JButton btnCompare = null;
    JButton btnCancel = null;
    
    JButton btnEARViewer = null;
    JButton btnEARCompare = null;
    
    static int intActualHeight = 0;
    static int intActualWidth = 0;
    
    JMenuItem mitemFileViewEARFile = null;
    JMenuItem mitemFileCompareEARFiles = null;
    JMenuItem mitemFileExit = null;
    
    JMenuItem mitemHelpContents = null;
    JMenuItem mitemHelpAbout = null;

    public static String strGlobleExcelReportLocation;
    public static String strGlobleHTMLReportLocation;
    public static String strGlobleTextReportLocation;
    public static String strGlobleTemperoryDirectoryLocation;
    
    public void showEARUtilityFrame()
    {
        try
        {
            WindowsLookAndFeel w = new WindowsLookAndFeel();
            UIManager.setLookAndFeel(w);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        //Initializing Log4J
        PatternLayout myLayout = new PatternLayout("%-4r [%t] %-5p %c %x - %m%n");
        ConsoleAppender consoleAppender = new ConsoleAppender(myLayout);
        RollingFileAppender rollingFileAppender = null;
        try
        {
            rollingFileAppender = new RollingFileAppender(myLayout,"\\log4joutput.log",true);
            rollingFileAppender.setMaxFileSize("100KB");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        log4jEARUtility = Logger.getLogger("EARUtility");
        log4jEARUtility.addAppender(consoleAppender);
        log4jEARUtility.addAppender(rollingFileAppender);
        log4jEARUtility.setLevel(Level.INFO);
        log4jEARUtility.info("Starting EAR Utility Program.");
        
        Toolkit tolkitEARCompare = Toolkit.getDefaultToolkit();
        Dimension dimEARCompare = tolkitEARCompare.getScreenSize();
        intActualHeight = dimEARCompare.height - 27;
        intActualWidth = dimEARCompare.width;
        
        frmEARUtility = new JFrame("EAR Utility 1.0");
        frmEARUtility.getContentPane().setBackground(Color.WHITE);
        frmEARUtility.setResizable(false);
        
        JLabel lblHeading = new JLabel("EAR Utility 1.1");
        Font fontHeading = new Font("Times New Roman", Font.BOLD, 35);
        lblHeading.setForeground(Color.BLUE);
        lblHeading.setFont(fontHeading);
        
        btnEARViewer = new JButton("<html><body><b><center>View<br>EAR");
        btnEARCompare = new JButton("<html><body><b><center>Compare<br>EAR");
        
        lblHeading.setBounds(60, 20, 300, 50);
        btnEARViewer.setBounds(50, 80, 100, 70);
        btnEARCompare.setBounds(200, 80, 100, 70);
        
        btnEARViewer.addActionListener(this);
        btnEARCompare.addActionListener(this);
        
        createMenuBar();
        
        frmEARUtility.setLayout(null);
        frmEARUtility.add(lblHeading);
        frmEARUtility.add(btnEARViewer);
        frmEARUtility.add(btnEARCompare);
        
        frmEARUtility.setSize(350, 250);
        frmEARUtility.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmEARUtility.setLocationRelativeTo(new JFrame());
        frmEARUtility.setVisible(true);
    }
    
    
    public void createMenuBar()
    {
        JMenuBar mbarJGUITester = new JMenuBar();

        JMenu menuFile = new JMenu("File");
        menuFile.setMnemonic('F');
        JMenu menuHelp = new JMenu("Help");
        menuHelp.setMnemonic('H');
        
        mbarJGUITester.add(menuFile);
        mbarJGUITester.add(menuHelp);
        
        mitemFileViewEARFile = new JMenuItem("View EAR File");
        mitemFileCompareEARFiles = new JMenuItem("Compare EAR Files");
        mitemFileExit = new JMenuItem("Exit");
        
        mitemFileViewEARFile.setMnemonic('V');
        mitemFileCompareEARFiles.setMnemonic('C');
        mitemFileExit.setMnemonic('x');
        
        mitemHelpContents = new JMenuItem("Contents");
        mitemHelpAbout  = new JMenuItem("About");
        
		mitemHelpContents.addActionListener(this);
		mitemHelpAbout.addActionListener(this);
		
        mitemHelpContents.setMnemonic('C');
        mitemHelpAbout.setMnemonic('A');        

        mitemFileViewEARFile.addActionListener(this);
        mitemFileCompareEARFiles.addActionListener(this);
        mitemFileExit.addActionListener(this);
        
        menuFile.add(mitemFileViewEARFile);
        
        menuFile.add(mitemFileCompareEARFiles);
        menuFile.add(mitemFileExit);
        
        menuHelp.add(mitemHelpContents);
        menuHelp.add(mitemHelpAbout);
        
        frmEARUtility.setJMenuBar(mbarJGUITester);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnEARCompare || e.getSource() == mitemFileCompareEARFiles)
        {
            showEARFilesDetailCollectingDialog();
        }
        else if(e.getSource() == btnEARViewer || e.getSource() == mitemFileViewEARFile)
        {
            String strFileToOpen = showEAROpeningFileChooser();
            if(strFileToOpen != null)
            {
                new EARFileViewingTool().showEARExploreDialog(strFileToOpen);
            }
        }
        else if(e.getSource() == btnBrowseFirstEarFile)
        {
            String strFileName = showEARSelectingFileChooser();
            if(strFileName != null)
            {
                txtFirstEARFile.setText(strFileName);
            }
        }
        else if(e.getSource() == btnBrowseSecondEarFile)
        {
            String strFileName = showEARSelectingFileChooser();
            if(strFileName != null)
            {
                txtSecondEARFile.setText(strFileName);
            }
        }
        else if(e.getSource() == btnCompare)
        {
            String strFirstEarFile = txtFirstEARFile.getText();
            String strSecondEarFile = txtSecondEARFile.getText();
            
            if((strFirstEarFile != null && strFirstEarFile.trim().length() <= 0))
            {
                JOptionPane.showMessageDialog(dlgEarCompareFileCollect, "Please select the frist file to compare");
                return;
            }
            if((strSecondEarFile != null && strSecondEarFile.trim().length() <= 0))
            {
                JOptionPane.showMessageDialog(dlgEarCompareFileCollect, "Please select the second file to compare");
                return;
            }
            if(strFirstEarFile.equals(strSecondEarFile))
            {
                JOptionPane.showMessageDialog(dlgEarCompareFileCollect, "Please select two different files to compare");
                return;
            }
            dlgEarCompareFileCollect.setVisible(false);
            new EARFilesComparingTool().compareFilesinEar("", "", strFirstEarFile, strSecondEarFile);
            
        }
        else if(e.getSource() == btnCancel)
        {
            dlgEarCompareFileCollect.setVisible(false);
        }
        else if(e.getSource() == mitemFileExit)
        {
            System.exit(0);
        }
        else if(e.getSource() == mitemHelpContents)
        {
            JOptionPane.showMessageDialog(frmEARUtility, "<html><body><B>Under Construction</B><BR>Will added in the next version", "Under Construction", JOptionPane.INFORMATION_MESSAGE);
        }
        else if(e.getSource() == mitemHelpAbout)
        {
            String strAboutDetails = new StringBuffer()
                .append("<html><body><B>EAR Utility</B></BR>Version 1.1")
                .append("<br><br><u><b>Developed By <br></u>")
                .append("A.Augustine Joseph<br></b>")
                .append("Cognizant Technology Solutions<br>")
                .append("<u>Augustine.Joseph@cognizant.com<br>").toString();
            JOptionPane.showMessageDialog(frmEARUtility, strAboutDetails
                , "About...", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    public void showEARFilesDetailCollectingDialog()
    {
        dlgEarCompareFileCollect = new JDialog(frmEARUtility, "Compare EAR File", true);
        
        txtFirstEARFile = new RoundTextField(0, Color.PINK, Color.WHITE, true);
        txtSecondEARFile = new RoundTextField(0, Color.PINK, Color.WHITE, true);
        
        txtFirstEARFile.setEditable(false);
        txtSecondEARFile.setEditable(false);
        
        btnBrowseFirstEarFile = new JButton("Browse...");
        btnBrowseSecondEarFile = new JButton("Browse...");
        btnCompare = new JButton("Compare");
        btnCancel = new JButton("Cancel");
        
        btnBrowseFirstEarFile.addActionListener(this);
        btnBrowseSecondEarFile.addActionListener(this);
        btnCompare.addActionListener(this);
        btnCancel.addActionListener(this);
        
        dlgEarCompareFileCollect.setLayout(null);
        
        dlgEarCompareFileCollect.getContentPane().add(txtFirstEARFile);
        dlgEarCompareFileCollect.getContentPane().add(txtSecondEARFile);
        dlgEarCompareFileCollect.getContentPane().add(btnBrowseFirstEarFile);
        dlgEarCompareFileCollect.getContentPane().add(btnBrowseSecondEarFile);
        dlgEarCompareFileCollect.getContentPane().add(btnCompare);
        dlgEarCompareFileCollect.getContentPane().add(btnCancel);
        
        
        txtFirstEARFile.setBounds(10, 10, 400, 20);
        btnBrowseFirstEarFile.setBounds(420, 10, 100, 20);
        txtSecondEARFile.setBounds(10, 40, 400, 20);
        btnBrowseSecondEarFile.setBounds(420, 40, 100, 20);
        btnCompare.setBounds(420, 70, 100, 20);
        btnCancel.setBounds(310, 70, 100, 20);
        
        dlgEarCompareFileCollect.setSize(540, 135);
        dlgEarCompareFileCollect.setLocationRelativeTo(frmEARUtility);
        dlgEarCompareFileCollect.setVisible(true);
    }

    public String showEARSelectingFileChooser()
    {
        JFileChooser filcsrSelectEARFile = new JFileChooser();
        filcsrSelectEARFile.setAcceptAllFileFilterUsed(false);
        
        filcsrSelectEARFile.setApproveButtonText("Select File");
        filcsrSelectEARFile.setApproveButtonMnemonic('S');
        filcsrSelectEARFile.setApproveButtonToolTipText("Select an EAR ,WAR ,JAR or ZIP File to Compare");
        filcsrSelectEARFile.setDialogTitle("Select an EAR ,WAR ,JAR or ZIP File to Compare");
        
        filcsrSelectEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Enterprise Archive (*.ear)", "ear"));
        filcsrSelectEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Web Archive (*.war)", "war"));
        filcsrSelectEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Java Archive (*.jar)", "jar"));
        filcsrSelectEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Zip Archive (*.zip)", "zip"));
        filcsrSelectEARFile.addChoosableFileFilter(new CustomFileChooserFilter("All Archive (*.ear, *.war, *.jar, &.zip)", "*"));

        filcsrSelectEARFile.showSaveDialog(dlgEarCompareFileCollect);
        
        // Get the currently selected file
        File fileSelected = filcsrSelectEARFile.getSelectedFile();
        String strSelectedFile = null;
        if(fileSelected != null)
        {
            strSelectedFile = fileSelected.toString();
//            System.out.println("strSelectedFile->" + strSelectedFile);
        }
        return strSelectedFile;
    }
    
    public String showEAROpeningFileChooser()
    {
        JFileChooser filcsrOpenEARFile = new JFileChooser();
        filcsrOpenEARFile.setAcceptAllFileFilterUsed(false);
        
        filcsrOpenEARFile.setApproveButtonText("Open File");
        filcsrOpenEARFile.setApproveButtonMnemonic('O');
        filcsrOpenEARFile.setApproveButtonToolTipText("Open an EAR ,WAR ,JAR or ZIP File to Explore");
        filcsrOpenEARFile.setDialogTitle("Open an EAR ,WAR ,JAR or ZIP File to Explore");
        
        filcsrOpenEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Enterprise Archive (*.ear)", "ear"));
        filcsrOpenEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Web Archive (*.war)", "war"));
        filcsrOpenEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Java Archive (*.jar)", "jar"));
        filcsrOpenEARFile.addChoosableFileFilter(new CustomFileChooserFilter("Zip Archive (*.zip)", "zip"));
        filcsrOpenEARFile.addChoosableFileFilter(new CustomFileChooserFilter("All Archive (*.ear, *.war, *.jar, &.zip)", "*"));
        
        filcsrOpenEARFile.showOpenDialog(frmEARUtility);
        
        // Get the currently selected file
        File fileToOpen = filcsrOpenEARFile.getSelectedFile();
        String strFileToOpen = null;
        if(fileToOpen != null)
        {
            strFileToOpen = fileToOpen.toString();
        }
        return strFileToOpen;
    }
    
    public void loadFromPropertyFile()
    {
        Properties propEARCompare = new Properties();
        String strDeletedFileProperties = "C:\\My Works\\Eclipse WorkSpace\\JavaWorkspace\\EARComp\\src\\EARCompare.properties";
        try
        {
            FileInputStream flinstDeleteProject = new FileInputStream(strDeletedFileProperties);
            propEARCompare.load(flinstDeleteProject);
            System.out.println("->" + propEARCompare);
            
            Enumeration enumEARCompare = propEARCompare.keys();
            while(enumEARCompare.hasMoreElements())
            {
                String strKey = enumEARCompare.nextElement().toString();
                if(strKey.equals("ExcelReport"))
                {
                    strGlobleExcelReportLocation = propEARCompare.get(strKey).toString();
                }
                else if(strKey.equals("HTMLReport"))
                {
                    strGlobleHTMLReportLocation = propEARCompare.get(strKey).toString();
                }
                else if(strKey.equals("TextReport"))
                {
                    strGlobleTextReportLocation = propEARCompare.get(strKey).toString();
                }
                else if(strKey.equals("TemperoryDirectory"))
                {
                    strGlobleTemperoryDirectoryLocation = propEARCompare.get(strKey).toString();
                }
            }            
            flinstDeleteProject.close();
        }
        catch(Exception re)
        {
            re.printStackTrace();
        }
    }
        
    public static void main(String[] args)
    {
        EARUtility objEARUtility = new EARUtility();
        objEARUtility.loadFromPropertyFile();
        objEARUtility.showEARUtilityFrame();
    }
}